/**
 * ref :: https://segmentfault.com/a/1190000003810652
 */
import 'fetch-ie8'
import 'es5-shim';
import 'fetch-detector'
import Promise from 'es6-promise';
import CONFIG from './config';

export default function (url, data = {}) {

    if (typeof data.body == 'object') {
        var tmp = [];
        for (var pro in data.body) {
            (!!data.body[pro]) && tmp.push(pro + '=' + data.body[pro]);
        }
        url += url.indexOf('?') > -1 ? '&' : '?';
        if (url.indexOf('?') > -1) {
            // console.log(url)
        }
        url += tmp.join('&');
        data.body = JSON.stringify(data.body)
    }
    if ((!data.method || data.method.toUpperCase() == 'GET') && data.body) {
        delete data.body;
    }
    return new Promise((resolve, reject)=> {
        fetch(url, Object.assign({
            credentials: 'include', //enable cookie
            method: 'GET',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json; charset=UTF-8'
            },
            cache: ''
        }, data))
            .then(res=> {
                if (res.headers.get("content-type") &&
                    res.headers.get("content-type").toLowerCase().indexOf("application/json") >= 0) {
                    return res.json()
                } else {
                    return res;
                }
            })
            .then(data=> {
                if ((data.status == 504) || (data.status == 500)) {
                    // alert(data.statusText);
                    console.log(JSON.stringify(data))
                } else if (data.result && '000000' == data.result.retCode) {
                    var tmp = {page: '', data: ''};
                    for (var pro in data.result.data) {
                        if (data.result.data[pro] instanceof Array) {
                            tmp[pro] = data.result.data[pro];
                            tmp.data = data.result.data[pro];
                            tmp.data = tmp.data.map((item, index) => {
                                if (typeof item == 'object') {
                                    item.index = index;
                                }
                                return item;
                            })
                        } else if (pro == 'page') {
                            tmp['pages'] = data.result.data[pro].totalPage;
                            tmp['total'] = data.result.data[pro].totalSize;
                            tmp['page'] = data.result.data[pro].currentPage;
                            tmp['size'] = data.result.data[pro].pageSize;
                            tmp['size'] = data.result.data[pro].pageSize;
                            tmp[pro + '_'] = data.result.data[pro];
                            tmp['page'] = data.result.data[pro].currentPage;
                        } else {
                            tmp[pro] = data.result.data[pro];
                        }
                    }
                    resolve(tmp)
                } else if (data.result && (data.result.retCode == '040003')) {
                    // alert(data.msg || '您可能无权访问或已经掉线!');
                    // window.location = '/login';
                } else {
                    // alert(data.result.reason);
                    console.log(data)
                    reject(data)
                }
            })
            .catch(e=> {
                new Error('Oops,error', e);
                console.error('Oops,error', e)
            })
    })

};